#include "Configuration.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>

using namespace NATSUME;

Configuration::Configuration(const std::string & filePath)
    : m_filePath(filePath)
{
    initConfigMap();
    initEN_StopWordList();
    initCN_StopWordList();
}

Configuration::~Configuration() { }



// 获取配置文件内容
std::map<std::string, std::string> & 
Configuration::getConfigMap() 
{
    return m_configMap;
}

// 获取中文停用词词集
std::set<std::string> &
Configuration::getCN_StopWordList()
{
    return m_cn_stopWordList;
}

// 获取英文停用词词集
std::set<std::string> &
Configuration::getEN_StopWordList()
{
    return m_en_stopWordList;
}



// 初始化配置文件内容
void Configuration::initConfigMap() 
{
    std::ifstream   file(m_filePath);
    if (! file.good()) {
        std::cerr << "open " << m_filePath << "error!" << std::endl;
        return;
    }

    file.seekg(0);
    std::string     text;
    while (std::getline(file, text)) {
        std::stringstream   line(text);
        std::string         key, value;

        while (line >> key >> value) {
            m_configMap.insert(std::make_pair(key, value));
        }
    }

    file.close();
}

// 初始化中文停用词词集
void Configuration::initCN_StopWordList() 
{
    std::ifstream   file(m_configMap["CN_STOP_WORD_PATH"]);
    if (! file.good()) {
        std::cerr << "open" << m_configMap["CN_STOP_WORD_PATH"] << "error!" << std::endl;
        return;
    }

    file.seekg(0);
    std::string     stopWord;
    while (file >> stopWord) {
        m_cn_stopWordList.insert(stopWord);
    }

    file.close();
}

// 初始化英文停用词词集
void Configuration::initEN_StopWordList() 
{
    std::ifstream   file(m_configMap["EN_STOP_WORD_PATH"]);
    if (! file.good()) {
        std::cerr << "open" << m_configMap["EN_STOP_WORD_PATH"] << "error!" << std::endl;
        return;
    }

    file.seekg(0);
    std::string     stopWord;
    while (file >> stopWord) {
        m_en_stopWordList.insert(stopWord);
    }

    file.close();
}
